import { getToken, removeToken, setToken } from '@/utils/auth'
import { defineStore } from 'pinia'

const useUserStore = defineStore('user', {
    // 状态
    state () {
        return {
            token: getToken() || '',
            roles: [] as Array<string>,
            user: {} as any,
            permissions: []
        }
    },
    getters: {
    },
    actions: {
        Login(userInfo: any) {
            return new Promise((resolve:any, reject:any) => {
                // login(userInfo).then((res: any) => {
                //     this.token = res.token
                //     setToken(res.token)
                //     resolve()
                // }).catch((error:any) => {
                //     reject(error)
                // })
            })
        },
        GetInfo () {
            return new Promise((resolve:any, reject:any) => {
                // getInfo().then((res:any) => {
                //     if (res.roles && res.roles.length > 0) {
                //         this.roles = res.roles
                //         this.permissions = res.permission
                //     } else {
                //         this.roles = ['ROLE_DEFAULT']
                //     }
                //     this.user = res.user
                //     resolve()
                // }).catch((error: any) => {
                //     reject(error)
                // })
            })
        },
        Logout() {
            return new Promise((resolve:any, reject:any) => {
                setTimeout(() => {
                    this.token = ''
                    this.roles = []
                    this.permissions = []
                    removeToken()
                    resolve()
                }, 500)
            })
        }
    }
})

export default useUserStore